package com.example.mysqlmycat.service;

import com.example.mysqlmycat.mode.User;
import com.example.mysqlmycat.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户业务层
 */
@Service
public class UserService {
    private static final Logger log = LoggerFactory.getLogger(UserService.class);

    /**
     * 业务层要调用持久层
     */

    @Autowired
    private UserRepository userRepository;

    public User saveUser(User user){
        try {
            //业务层掉用的添加方法，也就是JPA里的东西
            //保存用户如果没有就保存用户
            User save = userRepository.save(user);
            if (save == null){
                return null;
            }else {
                return save;
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(UserRepository.class.getName() + "save user is error");
            return null;
        }
    }
}
